<template>
    <div>
        <Row>
            <Col span="24"
                <Alert show-icon>
                    操作说明
                    <Icon type="ios-bulb-outline" slot="icon"></Icon>
                    <template slot="desc">
                        <alertcontent :content = "content"></alertcontent>
                    </template>
                </Alert>
            </Col>
        </Row>
        <Row>
            <Col span="24"
                <Alert show-icon>
                    分享图片
                    <Icon type="ios-bulb-outline" slot="icon"></Icon>
                    <template slot="desc">
                        用户选择分享图片
                    </template>
                </Alert>
            </Col>
        </Row>
        <Row>
            <Col span="12">
                <Form>
                    <FormItem v-for="(item,key) in imglist" :key="key" :label="item.label">
                        <uploadimg @uploadaction="uploadsuccess($event)" :imgurl="item.value" :tag="item.tag"></uploadimg>
                    </FormItem>
                    <FormItem style="text-indent:50px;">
                        <Button @click="savebasesetting" type="primary" :disabled="setAccess">保存</Button>
                    </FormItem>
                </Form>
            </Col>
        </Row>
    </div>
</template>
<script>
import alertcontent from '_c/alertcontent/alertcontent'
import uploadimg from '_c/uploadimg/uploadimg'
import { getConfig, setConfig, repaybank } from '@/api/data'
import { getAuthkey, getAccess, getUploadImgList } from '@/libs/util'
export default {
    name: 'sharesetting',
    data() {
        return {
            setAccess:true,
            content: ['配置相关图片', '加密参数配置'],
            imglist: [],
            uploadImgList:{share01: '', share02: '', share03: '', share04: '', share05: ''}
        }
    },
    components: {
        alertcontent, uploadimg
    },
    created() {
        this.setAccess = this.setBtnStatus()
        this.getsetting()
    },
    methods: {
        setBtnStatus: function(){
            let access = getAccess()
            if(access.length == 0){
                return true
            } else if (access.indexOf('admin') ==-1) {
                return true
            }else{
                return false
            }
        },
        cloneObj(obj){
            let newObj = JSON.parse(JSON.stringify(obj));
            return newObj
        },
        savebasesetting: function() {
            let clonedata = this.cloneObj(this.uploadImgList)
            for(let i in clonedata){
                if(!clonedata[i]){
                    delete clonedata[i]
                }
            }
            let reqdata = {
                auth_key: getAuthkey(),
                eidtconfig: JSON.stringify(clonedata)
            }
            setConfig(reqdata).then(res => {
                this.$Message.success('配置保存成功')
                this.getsetting()
            }, err => {
                this.$Message.error(err?err: '保存设置失败')
            })
        },
        getsetting: function() {
            let auth_key = getAuthkey()
            let data = { auth_key  }
            getConfig(data).then(res => {
                if(res.data.code !== 200){
                    this.$Message.error(res.data.error)
                    return
                }
                this.imglist = [
                    { label: '分享图片1：', value: res.data.data.share01?res.data.data.share01: '' , tag: 'share01'},
                    { label: '分享图片2：', value: res.data.data.share02?res.data.data.share02: '' , tag: 'share02'},
                    { label: '分享图片3：', value: res.data.data.share03?res.data.data.share03: '' , tag: 'share03'},
                    { label: '分享图片4：', value: res.data.data.share04?res.data.data.share04: '' , tag: 'share04'},
                    { label: '分享图片5：', value: res.data.data.share05?res.data.data.share05: '' , tag: 'share05'}]
            }, err => {
                this.$Message.error(err?err: '获取配置信息失败')
            })
        },
        uploadsuccess(data){
            this.uploadImgList = getUploadImgList(this.uploadImgList,data)
        }
    }
}
</script>
<style scoped>

</style>
